package com.ruoyi.dining.controller.exception.handler;

import com.ruoyi.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

@RestControllerAdvice
public class ParameterExceptionHandler {
    private static final Logger logger= LoggerFactory.getLogger(ParameterExceptionHandler.class);

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult parameterException(MethodArgumentNotValidException exception){
        List<ObjectError> allErrors = exception.getAllErrors();
        StringBuilder stringBuilder=new StringBuilder();
        for (ObjectError error : allErrors) {
            stringBuilder.append(error.getDefaultMessage()).append("; ");
        }
        logger.info("参数校验失败：{}",stringBuilder);
        return AjaxResult.warn(stringBuilder.toString());
    }
}
